<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" size="medium" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table
        :data="orderList"
        border stripe max-height="565">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" size="mini" v-if="scope.row.pay_status">未付款</el-tag>
            <el-tag type="success" size="mini" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dataFormat }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button type="primary" size="mini" @click="showEditDialog">编辑</el-button>
            <el-button type="success" size="mini" @click="showProgressDialog">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改地址的对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      @close="addressDialogClosed"
      width="40%" clearable>
      <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px"
               class="demo-ruleForm">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityDate">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="ProgressBoxDialogVisible"
      width="40%" clearable>
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityDate from './citydata'
import progress from './progress'

export default {
  name: 'Order',
  data() {
    return {
      // 获取订单列表查询参数对象
      queryInfo: {
        query: '',
        // 当前页数
        pagenum: 1,
        // 每页显示多少数据
        pagesize: 10
      },
      total: 0,
      // 订单列表
      orderList: [],
      // 控制修改地址对话框的显示与隐藏
      addressDialogVisible: false,
      // 编辑
      addressForm: {
        address1: [],
        address2: ''
      },
      // 编辑表单验证规则
      addressFormRules: {
        address1: [
          {required: true, message: '请选择省市区县', trigger: 'blur'}
        ],
        address2: [
          {required: true, message: '请填写详细地址', trigger: 'blur'}
        ]
      },
      cityDate: cityDate,
      // 物流进度对话框
      ProgressBoxDialogVisible: false,
      // 物流进度
      progressInfo: progress
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单数据
    async getOrderList() {
      const {data: res} = await this.$http.get('orders', {
        params: this.queryInfo
      })
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听 pagesize改变的事件
    handleSizeChange(newSize) {
      // console.log(`每页 ${newSize} 条`)
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听 pagenum页码值 改变事件
    handleCurrentChange(newPage) {
      // console.log(`当前页: ${newPage}`)
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 控制修改地址对话框的显示
    showEditDialog() {
      this.addressDialogVisible = true
    },
    // 监听修改地址对话框的关闭事件,清空输入
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 展示物流信息对话框
    async showProgressDialog() {
      // 供测试的物流单号：1106975712662
      // const {data: res} = await this.$http.get('/kuaidi/804909574412544580')
      // if (res.meta.status !== 200) {
      //   return this.$message.error(res.meta.msg)
      // }
      // this.progressInfo = res.data
      this.ProgressBoxDialogVisible = true
    }
  }
}
</script>

<style scoped lang="less">
.el-cascader {
  width: 80%;
}
</style>
